<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${cIntegralItem.id == null ? '添加' : '编辑'}积分项</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post"
                      action="${ctx}/member/integral/integralItem/saveOrUpdate.do">
                    <input type="hidden" name="id" value="${cIntegralItem.id}"/>
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择分社</label>
                        <div class="layui-inline">
                            <li class="form-item-inline">
                                <div class="layui-input-inline">
                                    <select id="memberGroupId" name="memberGroupId" lay-verify="memberGroupId" lay-filter="memberGroupId">
                                        <option value="">全部</option>
                                        <c:forEach var="memberGroup" items="${memberGroupList}">
                                            <option value="${memberGroup.id}"
                                                    ${cIntegralItem.createBy == memberGroup.id ?
                                                    'selected="selected"' : ''}>${memberGroup.realname}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                                <div class="layui-form-mid layui-word-aux">
                                    选择了分社，表示代表分社添加积分项，否则是当前机构添加积分项
                                </div>
                            </li>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">积分类型<span class="f-verify-red">*</span></label>
                        <div class="layui-inline">
                            <li class="form-item-inline">
                                <div class="layui-input-inline">
                                    <select id="integralTypes" name="typeId" lay-verify="typeId" lay-filter="integralTypes" ${category > 0 ? 'disabled="disabled"' : ''}>
                                        <option value="">全部</option>
                                        <c:forEach var="type" items="${cIntegralTypes}">
                                            <option value="${type.id}"
                                                    data-iType="${type.category}" ${cIntegralItem.typeId == type.id ?
                                                    'selected="selected"' : ''}>${type.name}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </li>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">名称<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" lay-verify="name" autocomplete="off"
                                   class="layui-input" value="${cIntegralItem.name}" maxlength="30"
                                   style="width: 400px; float: left; margin-right: 10px;"
                            >
                        </div>
                    </div>
                    <div class="layui-form-item" id = "recommendGradeDiv" ${category == 1 ? 'style="display: block"' : 'style="display: none"'}>
                        <div class="layui-inline">
                            <label class="layui-form-label">推荐等级</label>
                            <div class="layui-input-inline">
                                <input type="radio" name="recommendGrade" value="1"  <c:if test="${isExistGradeOrdinary}"> disabled="true" </c:if> title="默认"
                                ${cIntegralItem.recommendGrade == '1' ? 'checked="checked"' : ''}>
                                <input type="radio" name="recommendGrade" value="2" <c:if test="${cIntegralItem.recommendGrade == '1'}"> disabled="true" </c:if> title="其他"
                                ${cIntegralItem.recommendGrade == null || cIntegralItem.recommendGrade != '1' ? 'checked="checked"' : ''}>
                            </div>
                            <div class="layui-form-mid layui-word-aux">
                                默认等级：用户成功邀请新用户加入分社获得的推荐积分，且只能存在一个。<br/>
                                其他等级：分社管理员根据被推荐人不同职位给推荐人添加相应的积分。
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item" id = "giftGradeDiv" ${category == 2 ? 'style="display: block"' : 'style="display: none"'}>
                        <div class="layui-inline">
                            <label class="layui-form-label">赠送等级</label>
                            <div class="layui-input-inline">
                                <input type="radio" name="giftGrade" value="1"  <c:if test="${isExistGiftOrdinary}"> disabled="true" </c:if> title="默认"
                                ${cIntegralItem.giftGrade == '1' ? 'checked="checked"' : ''}>
                                <input type="radio" name="giftGrade" value="2" <c:if test="${cIntegralItem.giftGrade == '1'}"> disabled="true" </c:if> title="其他"
                                ${cIntegralItem.giftGrade == null || cIntegralItem.giftGrade != '1' ? 'checked="checked"' : ''}>
                            </div>
                            <div class="layui-form-mid layui-word-aux">
                                默认等级：用户第一次加入分社获得的赠送积分，且只能存在一个。<br/>
                                其他等级：用户通过其他途径获得的赠送积分。
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分数<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <input type="number" name="itemFraction"
                                   class="layui-input" value="${cIntegralItem.itemFraction}"
                                   style="width: 200px;  float: left; margin-right: 10px;" lay-verify="itemFraction">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit
                               lay-filter="*">立即提交</a>
                            <a href="${ctx}/member/integral/integralItem/list.do"
                               class="layui-btn layui-btn-primary">取消</a>
                        </div>
                    </div>
            </form>
        </div>
</div>
<%@include file="../include/footer.jsp" %>
</section>
</div>
<script>
    var isExistGradeOrdinary = ${isExistGradeOrdinary}
    txz.initHeader({
        nav: [{
            name: '积分项',
            href: '${ctx}/member/integral/integralItem/list.do'
        }, {
            name: '${cIntegralItem.id == null ? '添加' : '编辑'}积分项',
            curr: true
        }],
        btns: [{
            type: 'back'
        }]
    });
    var form, laydate, formcache;
    $(function () {
        layui.use(['form', 'laydate'], function () {
            form = layui.form, laydate = layui.laydate;

            // 行业
            form.on('select(integralTypes)', function (data) {
                var iType = $(data.elem).find("option:selected").attr("data-iType");
                if(iType==1){
                    $("#giftGradeDiv").css('display','none');
                    $("#recommendGradeDiv").css('display','block');
                }else if (iType==2) {
                    $("#recommendGradeDiv").css('display','none');
                    $("#giftGradeDiv").css('display','block');
                }else{
                    $("#recommendGradeDiv").css('display','none');
                    $("#giftGradeDiv").css('display','none');
                }

                form.render('select');
            });

            //自定义验证规则
            form.verify({
                name: function (value) {
                    if (value.trim() == "") {
                        return '分类名不能为空';
                    }
                },
                itemFraction: function (value) {
                    if (value.trim() == "") {
                        return "请填写分数";
                    }
                    if (value <= 0) {
                        return "请填写正确的分数";
                    }
                }
            });
            //监听提交
            form.on('submit', function (data) {
                $("#integralTypes").removeAttr("disabled")
                submitForm(data);
            });
        });
    });

    function submitForm(data) {
        txz.submitObject(data.elem, function (callBack) {
            var formData = getFormData();
            var action = $("#myForm").attr("action");
            txz.ajaxRequest({
                method: 'post',
                url: action,
                saveCache: true,
                cacheObj: formcache,
                params: formData,
                callBack: function (res) {
                    typeof callBack === 'function' && callBack();
                    if (res.success) {
                        util.layerMsgSuccess("提交成功", function () {
                            location.href = "${ctx}/member/integral/integralItem/list.do";
                        });
                    } else {
                        util.layerMsgError(res.description)
                    }
                }
            });
        })
    }

    function getFormData() {
        var formData = util.serializeForm($('#myForm').serializeArray());
        return formData;
    }
</script>
</body>
</html>